package com.zhuangfei.timetable.listener;

import com.zhuangfei.timetable.ResourceTable;
import com.zhuangfei.timetable.utils.ColorUtils;
import com.zhuangfei.timetable.utils.ElementUtil;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;

/**
 * 控件实现的一个可以显示时间的侧边栏适配器
 * Created by Liu ZhuangFei on 2018/6/18.
 */

public class OnSlideBuildAdapter implements ISchedule.OnSlideBuildListener {

    // 时刻，每个元素保存每节课的开始时间
    protected String[] times;

    // 节次文本的颜色、字号
    protected int textColor = Color.BLACK.getValue();
    protected int textSize = 14;

    // 时刻文本的颜色、字号
    protected int timeTextSize = 12;
    protected int timeTextColor = Color.GRAY.getValue();

    // 侧边栏背景色
    protected int background = Color.WHITE.getValue();
    protected float alpha = 1;

    /**
     * 设置时刻数组
     *
     * @param times 时刻数组
     * @return 显示时间的侧边栏适配器对象自身
     */
    public OnSlideBuildAdapter setTimes(String[] times) {
        this.times = times;
        return this;
    }

    public OnSlideBuildAdapter setBackground(int backgroundColor) {
        this.background = backgroundColor;
        return this;
    }

    /**
     * 获取时刻数组
     *
     * @return 时刻数组
     */
    public String[] getTimes() {
        return times;
    }


    /**
     * 设置节次文本颜色
     *
     * @param textColor 指定颜色
     * @return 显示时间的侧边栏适配器对象自身
     */
    public OnSlideBuildAdapter setTextColor(int textColor) {
        this.textColor = textColor;
        return this;
    }

    /**
     * 设置节次文本的大小
     *
     * @param textSize 指定字号
     * @return 显示时间的侧边栏适配器对象自身
     */
    public OnSlideBuildAdapter setTextSize(int textSize) {
        this.textSize = textSize;
        return this;
    }

    /**
     * 设置节次时间的文本颜色
     *
     * @param timeTextColor 颜色
     * @return 显示时间的侧边栏适配器对象自身
     */
    public OnSlideBuildAdapter setTimeTextColor(int timeTextColor) {
        this.timeTextColor = timeTextColor;
        return this;
    }

    /**
     * 设置节次时间的文本大小
     *
     * @param timeTextSize 字号
     * @return 显示时间的侧边栏适配器对象自身
     */
    public OnSlideBuildAdapter setTimeTextSize(int timeTextSize) {
        this.timeTextSize = timeTextSize;
        return this;
    }

    @Override
    public Component getView(int pos, LayoutScatter inflater, int itemHeight, int marTop) {
        Component view = inflater.parse(ResourceTable.Layout_item_slide_time, null, false);
        Text numberTextView = (Text) view.findComponentById(ResourceTable.Id_item_slide_number);
        Text timeTextView = (Text) view.findComponentById(ResourceTable.Id_item_slide_time);
        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
                itemHeight);
        lp.setMargins(0, marTop, 0, 0);
        view.setLayoutConfig(lp);

        numberTextView.setText((pos + 1) + "");
        numberTextView.setTextSize(textSize, Text.TextSizeType.FP);
        numberTextView.setTextColor(new Color(textColor));

        if (times == null) {
            timeTextView.setText("");
        }
        if (times != null && pos >= 0 && pos < times.length) {
            timeTextView.setText(times[pos]);
            timeTextView.setTextColor(new Color(timeTextColor));
            timeTextView.setTextSize(timeTextSize, Text.TextSizeType.FP);
        }
        return view;
    }

    @Override
    public void onInit(DirectionalLayout layout, float alpha) {
        this.alpha = alpha;
        int alphaColor = ColorUtils.alphaColor(background, alpha);
        if (layout != null) {
            layout.setBackground(ElementUtil.getShapeElementWith8(alphaColor));
        }
    }
}
